package com.github.catvod.spider.merge;

import android.net.Uri;
import java.nio.charset.Charset;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.regex.Pattern;
import org.json.JSONObject;

/* loaded from: classes.dex */
public class CE {
    private static final Pattern h = Pattern.compile("http((?!http).)*?default\\.365yg\\.com/.*|http((?!http).){26,}?\\.(m3u8|mp4|flv|avi|mkv|rm|wmv|mpg|m4a|mp3)\\?.*|http((?!http).){26,}\\.(m3u8|mp4|flv|avi|mkv|rm|wmv|mpg|m4a|mp3)|http((?!http).){26,}?/m3u8\\?pt=m3u8.*|http((?!http).)*?default\\.ixigua\\.com/.*|http((?!http).)*?cdn-tos[^\\?]*|http((?!http).)*?/obj/tos[^\\?]*|http.*?/player/m3u8play\\.php\\?url=.*|http.*?/player/.*?[pP]lay\\.php\\?url=.*|http.*?/playlist/m3u8/\\?vid=.*|http.*?\\.php\\?type=m3u8&.*|http.*?/download.aspx\\?.*|http.*?/api/up_api.php\\?.*|https.*?\\.66yk\\.cn.*|http((?!http).)*?netease\\.com/file/.*");
    public static Charset V0 = Charset.forName("UTF-8");
    public static Charset j = Charset.forName("iso-8859-1");
    public static Charset q = Charset.forName("gb2312");

    public static JSONObject BQ(String str, String str2) {
        JSONObject jSONObject = new JSONObject(str2);
        String string = jSONObject.getString("url");
        if (string.startsWith("//")) {
            string = "https:" + string;
        }
        if (!string.startsWith("http")) {
            return null;
        }
        if ((string.equals(str) && (oC(string) || !I(string))) || q(str, string)) {
            return null;
        }
        JSONObject jSONObject2 = new JSONObject();
        String optString = jSONObject.optString("user-agent", "");
        if (optString.trim().length() > 0) {
            jSONObject2.put("User-Agent", " " + optString);
        }
        String optString2 = jSONObject.optString("referer", "");
        if (optString2.trim().length() > 0) {
            jSONObject2.put("Referer", " " + optString2);
        }
        JSONObject j2 = j(jSONObject2, str, string);
        JSONObject jSONObject3 = new JSONObject();
        jSONObject3.put("header", j2);
        jSONObject3.put("parse", 0);
        jSONObject3.put("jx", 0);
        jSONObject3.put("url", string);
        return jSONObject3;
    }

    public static boolean I(String str) {
        return h.matcher(str).find() && !str.contains(".js");
    }

    public static String V0(String str) {
        try {
            byte[] digest = MessageDigest.getInstance("SHA-1").digest(str.getBytes());
            StringBuilder sb = new StringBuilder();
            for (byte b : digest) {
                String hexString = Integer.toHexString(b & 255);
                if (hexString.length() == 1) {
                    sb.append('0');
                }
                sb.append(hexString);
            }
            return sb.toString();
        } catch (NoSuchAlgorithmException e) {
            throw new RuntimeException("SHA-1 algorithm not found", e);
        }
    }

    public static String h(String str, Charset charset) {
        try {
            byte[] digest = MessageDigest.getInstance("MD5").digest(str.getBytes(charset));
            StringBuilder sb = new StringBuilder();
            for (byte b : digest) {
                String hexString = Integer.toHexString(b & 255);
                if (hexString.length() < 2) {
                    sb.append(0);
                }
                sb.append(hexString);
            }
            return sb.toString().toLowerCase();
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
            return "";
        }
    }

    public static JSONObject j(JSONObject jSONObject, String str, String str2) {
        if (jSONObject == null) {
            jSONObject = new JSONObject();
        }
        if (str.contains("www.mgtv.com")) {
            jSONObject.put("Referer", " ");
            jSONObject.put("User-Agent", " Mozilla/5.0");
        } else if (str2.contains("titan.mgtv")) {
            jSONObject.put("Referer", " ");
            jSONObject.put("User-Agent", " Mozilla/5.0");
        } else if (str.contains("bilibili")) {
            jSONObject.put("Referer", " https://www.bilibili.com/");
            jSONObject.put("User-Agent", " Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/94.0.4606.54 Safari/537.36");
        }
        return jSONObject;
    }

    public static boolean oC(String str) {
        try {
            String host = Uri.parse(str).getHost();
            String[] strArr = {"iqiyi.com", "v.qq.com", "youku.com", "le.com", "tudou.com", "mgtv.com", "sohu.com", "acfun.cn", "bilibili.com", "baofeng.com", "pptv.com", "1905"};
            for (int i = 0; i < 12; i++) {
                if (host.contains(strArr[i]) && (!"iqiyi.com".equals(strArr[i]) || str.contains("iqiyi.com/a_") || str.contains("iqiyi.com/w_") || str.contains("iqiyi.com/v_"))) {
                    return true;
                }
            }
            return false;
        } catch (Exception unused) {
            return false;
        }
    }

    public static boolean q(String str, String str2) {
        return str2.contains("973973.xyz") || str2.contains(".fit:");
    }
}
